package com.wh1200.mybatis.xmlless.config

import com.wh1200.mybatis.xmlless.enums.XmllessDbType
import com.wh1200.xmlless.test.model.AppCluster
import com.wh1200.xmlless.test.dao.AppClusterDAO
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import kotlin.reflect.jvm.javaMethod
import kotlin.test.assertContains
import kotlin.test.assertNotNull


/**
 *
 * Created by 吴昊 on 2018-12-09.
 *
 * @author 吴昊
 * @since 0.0.1
 */
class AppClusterDAOResolverTest : BaseResolverTest(
    AppClusterDAO::class.java
) {

  @Test
  fun resolveFindAllPage() {
    val query = createQueryForMethod(AppClusterDAO::findAll.javaMethod!!)
    query.exception?.printStackTrace()
    val sql = query.sql
    assertNotNull(sql)
    assertContains(sql, "<foreach collection=\"pageable.sort.get().toArray()\" item=\"item\" separator=\",\">")
    assertContains(sql, "<if test=\"item.isAscending\">ASC</if><if test=\"item.isDescending\">DESC</if>")
    if (dbType == XmllessDbType.MYSQL) {
      assertContains(sql, "LIMIT #{pageable.offset}, #{pageable.pageSize}")
    } else if (dbType == XmllessDbType.POSTGRE_SQL) {
      assertContains(sql, "LIMIT #{pageable.pageSize} OFFSET #{pageable.offset}")
    }
  }

  @Test
  @DisplayName("被@JoinTableColumn注解的字段不参与持久化")
  fun save() {
    val query = createQueryForMethod(AppClusterDAO::save.javaMethod!!)
    val sql = query.sql
    assertNotNull(sql)
    assert(!sql.contains("appName"))
  }

}
